home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Online / Qpopper / pop_xmit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-13  |  3.9 KB  |  142 lines

  1. /*
  2.  * Copyright (c) 1989 Regents of the University of California.
  3.  * All rights reserved.  The Berkeley software License Agreement
  4.  * specifies the terms and conditions for redistribution.
  5.  */
  6.  
  7. #ifndef lint
  8. static char copyright[] = "Copyright (c) 1990 Regents of the University of California.\nAll rights reserved.\n";
  9. static char SccsId[] = "@(#)@(#)pop_xmit.c    2.1  2.1 3/18/91";
  10. #endif
  11.  
  12. #include <stdio.h>
  13. #include <sys/types.h>
  14. #include <sys/file.h>
  15.  
  16. #ifdef POPSCO
  17. # define __SCO_WAIT3__
  18. # include <fcntl.h>
  19. #endif
  20.  
  21. #include <sys/wait.h>
  22.  
  23. #if defined(SOLARIS2) || defined(UNIXWARE) || defined(AIX) || defined(PTX) \
  24.     || defined(AUX)
  25. #include <sys/stat.h>
  26. #include <fcntl.h>
  27. #endif
  28.  
  29. #include "popper.h"
  30.  
  31. /*
  32.  *  xmit:   POP XTND function to receive a message from 
  33.  *          a client and send it in the mail
  34.  */
  35.  
  36. pop_xmit (p)
  37. POP     *   p;
  38. {
  39.     FILE                *   tmp;                    /*  File descriptor for 
  40.                                                         temporary file */
  41.     int                tfn;            
  42.     char                    buffer[MAXLINELEN];     /*  Read buffer */
  43.     char                    temp_xmit[MAXDROPLEN];  /*  Name of the temporary 
  44.                                                         filedrop */
  45. #ifdef NEXT
  46.     union    wait        stat;
  47. #else
  48.     int                stat;
  49. #endif
  50.     PID_T                   id, pid;
  51.  
  52.     /*  Create a temporary file into which to copy the user's message */
  53.  
  54.     strncpy(temp_xmit, POP_TMPXMIT, sizeof(temp_xmit));
  55. #ifdef DEBUG
  56.     if(p->debug)
  57.         pop_log(p,POP_DEBUG,
  58.             "Creating temporary file for sending a mail message \"%s\"",
  59.                 temp_xmit);
  60. #endif
  61.     if (((tfn=mkstemp(temp_xmit)) == -1) ||
  62.     ((tmp=fdopen(tfn, "w+")) == NULL)) {    /* failure, bail out    */
  63.         return (pop_msg(p,POP_FAILURE,
  64.             "Unable to create temporary message file \"%s\", errno = %d",
  65.                 temp_xmit, errno));
  66.     }
  67.  
  68.     /*  Tell the client to start sending the message */
  69.     pop_msg(p,POP_SUCCESS,"Start sending the message.");
  70.  
  71.     /*  Receive the message */
  72. #ifdef DEBUG
  73.     if(p->debug)pop_log(p,POP_DEBUG,"Receiving mail message");
  74. #endif
  75.     while (fgets(buffer,MAXLINELEN,p->input)){
  76.         /*  Look for initial period */
  77. #ifdef DEBUG
  78.         if(p->debug)pop_log(p,POP_DEBUG,"Receiving: \"%s\"",buffer);
  79. #endif
  80.         if (*buffer == '.') {
  81.             /*  Exit on end of message */
  82.             if (strcmp(buffer,".\r\n") == 0)
  83.              break;
  84.         /* sendmail will not remove escaped .. */
  85.         else if (buffer[1] == '.') {
  86.         (void)fputs (&buffer[1], tmp);
  87.         } else {
  88.         (void)fputs (buffer, tmp);
  89.         }
  90.         } else
  91.         (void)fputs (buffer, tmp);
  92.     }
  93.     (void)fclose (tmp);
  94.  
  95. #ifdef DEBUG
  96.     if(p->debug)pop_log(p,POP_DEBUG,"Forking for \"%s\"",MAIL_COMMAND);
  97. #endif
  98.     /*  Send the message */
  99.     switch (pid = fork()) {
  100.         case 0:
  101.         /*  Open the log file */
  102.         (void)closelog();
  103. #ifdef SYSLOG42
  104.         (void)openlog(p->myname,0);
  105. #else
  106.         (void)openlog(p->myname,LOG_PID,POP_FACILITY);
  107. #endif
  108.         pop_log(p, POP_DEBUG,
  109.             "Pop transmit from \"%s\" on \"%s\"", p->user, p->client);
  110.  
  111.             (void)fclose (p->input);
  112.             (void)fclose (p->output);       
  113.             (void)close(0);
  114.             if (open(temp_xmit,O_RDONLY,0) < 0)
  115.         (void)_exit(1);
  116.             (void)execl (MAIL_COMMAND,"send-mail","-t","-oem",NULLCP);
  117.             (void)_exit(1);
  118.         case -1:
  119.             if (!p->debug) (void)unlink (temp_xmit);
  120.             return (pop_msg(p,POP_FAILURE, "Unable to execute \"%s\" (%d)",
  121.                             MAIL_COMMAND, errno));
  122.         default:
  123.  
  124. #ifdef NEXT
  125.             while((id = wait(&stat)) >=0 && id != pid);
  126. #else
  127.             id = waitpid(pid, &stat, 0);
  128. #endif
  129.             if (!p->debug) (void)unlink (temp_xmit);
  130.  
  131. #ifdef NEXT
  132.             if (!WIFEXITED (stat))
  133. #else
  134.             if ((!WIFEXITED (stat)) || (WEXITSTATUS (stat) != 0))
  135. #endif
  136.                 return (pop_msg(p,POP_FAILURE,"Unable to send message"));
  137.  
  138.             return (pop_msg (p,POP_SUCCESS,"Message sent successfully"));
  139.     }
  140. }
  141.  
  142.